import WebscoketGlobalHandle from "@/layouts/WebscoketGlobalHandle";
import {getRequestInstance} from "@@/exports";
import {ProProvider} from '@ant-design/pro-components';
import {StompClientContext, useStompClient} from "kf-web-library";
import React, {useContext} from 'react';
import {useModel} from "umi";
import {bizKfProValueTypeMap} from "@/components";

const WebscoketGlobalWrap: React.FC<any> = (props) => {

  const {initialState, setInitialState} = useModel('@@initialState');
  const authToken = initialState?.currentAuthInfo?.token;
  const proProviderValues = useContext(ProProvider);


  const enhanceProComp = {
    ...proProviderValues,
    valueTypeMap: bizKfProValueTypeMap(),
  };
  /**
   * webscoket
   */
  const stompClient = useStompClient({
    url: `${getRequestInstance().defaults.baseURL}/kf/ws`,
    authToken: authToken,
  });

  return (
    <ProProvider.Provider value={enhanceProComp}>
      <StompClientContext.Provider value={stompClient}>
        {props.children}
        <WebscoketGlobalHandle/>
      </StompClientContext.Provider>
    </ProProvider.Provider>
  );
};

export default WebscoketGlobalWrap;
